﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.stamm;
using personalplaner.dataprovider;
using personalplaner.common;
using System.Diagnostics;

namespace personalplaner.servicemodel.dataaccess
{
    public interface IAnstellungDataAccess
    {
        #region Get Methods

        /// <summary>
        /// gets a list containing all <see cref="Anstellung"/>
        /// </summary>
        /// <param name="addEmptyRow"></param>
        /// <returns></returns>
        IEnumerable<Anstellung> GetAnstellungen(bool addEmptyRow = false);

        Anstellung GetPersonAnstellung(int prsid);

        bool GetIsAnstellungUsed(int anstellungid);

        #endregion

        #region Set Methods

        /// <summary>
		/// saves a new anstellung in the database
		/// </summary>
		/// <param name="anstellung"></param>
		/// <returns></returns>
		bool SetAnstellung(Anstellung anstellung);

        void SetPersonAnstellung(int prsid, int anstellungid, DateTime von, DateTime bis);

        #endregion

        #region Update Methods

        bool UpdateAnstellung(Anstellung anstellung);

        #endregion

        #region Delete Methods

        bool DeleteAnstellung(int anstellung);

        #endregion
    }

    public class AnstellungDataAccess : BaseDataAccess, IAnstellungDataAccess
    {
        public AnstellungDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

        #region Get Methods

        /// <summary>
        /// gets a list containing all <see cref="Anstellung"/>
        /// </summary>
        /// <param name="addEmptyRow"></param>
        /// <returns></returns>
        public IEnumerable<Anstellung> GetAnstellungen(bool addEmptyRow = false)
        {
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AnstellungeService.GetAnstellungen(Settings.DatabaseSettings, Settings.Mandant, addEmptyRow);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
                    Console.Write(e);
				}

				return null;
			}
        }

        public Anstellung GetPersonAnstellung(int prsid)
        {
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AnstellungeService.GetPersonAnstellung(Settings.DatabaseSettings, prsid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
                    Console.Write(e);
				}

				return null;
			}
        }

        public bool GetIsAnstellungUsed(int anstellungid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.AnstellungeService.GetIsAnstellungUsed(Settings.DatabaseSettings, anstellungid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error

                    Console.Write(e);
                    return false;
                }
            }
        }

        #endregion

        #region Set Methods

        /// <summary>
		/// saves a new anstellung in the database
		/// </summary>
		/// <param name="anstellung"></param>
		/// <returns></returns>
		public bool SetAnstellung(Anstellung anstellung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AnstellungeService.SetAnstellung(Settings.DatabaseSettings, Settings.Mandant, anstellung);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error

					Debug.Write(e);
					return false;
				}
			}
		}

    	public void SetPersonAnstellung(int prsid, int anstellungid, DateTime von, DateTime bis)
        {
			using (new WaitCursor())
			{
				try
				{
					DataServices.AnstellungeService.SetPersonAnstellung(Settings.DatabaseSettings, prsid, anstellungid, von, bis);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}
			}
		}

		#endregion

		#region Update Methods

		public bool UpdateAnstellung(Anstellung anstellung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.AnstellungeService.UpdateAnstellung(Settings.DatabaseSettings, anstellung);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error

					Debug.Write(e);
					return false;
				}
			}
		}

    	#endregion

        #region Delete Methods

        public bool DeleteAnstellung(int anstellung)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.AnstellungeService.DeleteAnstellung(Settings.DatabaseSettings, anstellung);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error

                    Debug.Write(e);
                    return false;
                }
            }
        }

        #endregion
	}
}
